package com.card.manager.services.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.card.framework.mybatis.mapper.BatchCallBack;
import com.card.framework.mybatis.mapper.MyFrameworkSupperMapper;
import com.card.framework.mybatis.optimist.ModifiedByAnotherUserException;
import com.card.framework.page.model.PageModel;
import com.card.manager.dao.AcbMapper;
import com.card.manager.domain.Aca;
import com.card.manager.domain.Acb;
import com.card.manager.domain.AcbExample;
import com.card.manager.domain.AcbExample.Criteria;
import com.card.manager.services.AcbService;
@Service("AcbServiceImpl")
public class AcbServiceImpl extends MyFrameworkSupperMapper implements AcbService {

	@Override
	public Map<String, Object> selectAcbForPage(Map<String,String> param, PageModel pageModel) {
		AcbMapper mapper = this.getMapper(AcbMapper.class);
		AcbExample acbExample = new AcbExample();
		//AcbExample.createCriteria().andUsernameIsNotNull();
		//AcbExample.createCriteria().andUsernameEqualTo(Acb.getUsername());
		if(pageModel.getRows() != 0)
			acbExample.setPage(pageModel);
		if(StringUtils.isNotEmpty(pageModel.getSort())) 
			acbExample.setOrderByClause(pageModel.getSort()+" "+pageModel.getOrder());
//		if(StringUtils.isNotEmpty(acb.getUsername())) 
//		acbExample.createCriteria().andUsernameEqualTo(acb.getUsername());
		Criteria criteria = acbExample.createCriteria();
		if(StringUtils.isNotBlank(param.get("idnumber"))){
			criteria.andAaaOwnerCardLike("%" + param.get("idnumber") + "%");
		}
		if(StringUtils.isNotBlank(param.get("abaid"))){
			criteria.andAbaidLike("%" + param.get("abaid") + "%");
		}
		if(StringUtils.isNotBlank(param.get("namesimp"))){
			criteria.andAabNamesimpLike("%" + param.get("namesimp") + "%");
		}
		if(StringUtils.isNotBlank(param.get("serialnumber"))){
			criteria.andSerialnumberLike("%" + param.get("serialnumber") + "%");
		}
		if(StringUtils.isNotBlank(param.get("dateFrom"))){
			try {
				criteria.andReturntimeGreaterThanOrEqualTo(DateUtils.parseDate(param.get("dateFrom"), new String[]{"yyyy-MM-dd"}));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if(StringUtils.isNotBlank(param.get("dateTo"))){
			try {
				criteria.andReturntimeLessThanOrEqualTo(DateUtils.parseDate(param.get("dateTo"), new String[]{"yyyy-MM-dd"}));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		criteria.andDelflgEqualTo((byte)1);
		List<Acb> list = mapper.selectByExampleFetchJoin(acbExample);
		Map<String, Object> modelMap = new HashMap<String, Object>();  
	    modelMap.put("total", pageModel.getTotalCount());  
	    modelMap.put("rows", list); 
	    return modelMap;
	}

	@Override
	public void deleteAcbs(final List<Acb> acb)
			throws ModifiedByAnotherUserException, Exception {
		batchSubmit(AcbMapper.class,new BatchCallBack() {
			@Override
			public <E> void batchCallBack(E mapper) {
				if(acb != null && acb.size() > 0) {
					for(Acb a :acb){
						a.setDelflg((byte)0);
						((AcbMapper) mapper).updateByPrimaryKeySelective(a);
					}
				}
			}
		});
	}
	@Override
	@Transactional
	public void addOrUpdate(Acb acb) {
		try{
			AcbMapper mapper = this.getMapper(AcbMapper.class);
			//Aaa queryResult = mapper.selectByPrimaryKey(aaa.getUserid());
			if(acb.getId()==null){
				mapper.insert(acb);
			}else
			{
				AcbExample acbExample = new AcbExample();
				acbExample.createCriteria().andIdEqualTo(acb.getId());
				mapper.updateByExample(acb, acbExample);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public Acb getById(String id) {
		AcbMapper mapper = this.getMapper(AcbMapper.class);
	    return mapper.selectByPrimaryKey(id);
	}

	
}
